/*******************************************************************************

Copyright (c) 2003, Robert Cowham and Vaccaperna Systems Ltd.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1.  Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

2.  Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL VACCAPERNA SYSTEMS LTD. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

/*******************************************************************************
 * Name:	p4clientuser.h
 *
 * Author:	Robert Cowham <robert@vaccaperna.co.uk>
 *
 * Description: 
 *		COM bindings for the Perforce API. User interface class
 * 		for getting Perforce results into COM.
 *
 ******************************************************************************/

#if !defined(AFX_p4clientuser_H__C7A92243_2012_4DCA_8EB2_2E73ACA85A30__INCLUDED_)
#define AFX_p4clientuser_H__C7A92243_2012_4DCA_8EB2_2E73ACA85A30__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "stdafx.h"

#ifndef _P4API_H_
#define _P4API_H_

#ifdef SetPort		// Windows bug
#undef SetPort
#endif

// P4 API files
#include "PerforceAPI.h"
# include "enviro.h"
# include "signaler.h"

# include "msgclient.h"
# include "spec.h"
# include "diff.h"

#endif // _P4API_H_

#include "StrBufArray.h"
#include <string>
using std::wstring;

class p4ClientUser : public ClientUser
{
public:
	p4ClientUser();
	virtual ~p4ClientUser();

	virtual void Reset();
	virtual void HandleError(Error *err);
	virtual void ErrorPause(char *errBuf, Error *e);
	virtual void OutputInfo(char level, char *data);
	virtual void OutputError(char *errBuf);
	virtual void OutputWarning(char *errBuf);

	virtual void OutputText(char *data, int len);
	virtual void OutputBinary(char *data, int len);
	virtual void Diff( FileSys *f1, FileSys *f2, int doPage, 
                char *diffFlags, Error *e );

	virtual void Prompt( const StrPtr &msg, StrBuf &rsp, 
				int noEcho, Error *e );
	virtual void OutputStat(StrDict *results);
	virtual void InputData(StrBuf *strbuf, Error *e);

	virtual BSTR GetVar(LPCSTR varName);
	virtual int VarExists(LPCSTR varName);
	virtual void SetVar(LPCSTR varName, BSTR newVal);
	void SetArrayVar(LPCSTR varName, StrBufArray *newVal);
	StrBufArray *GetArrayVar(LPCSTR varName);

	virtual void TranslateCharset(int newVal);
	virtual int TranslateCharset();
	LPCSTR p4ClientUser::TranslateFromBSTR(BSTR bs);
	LPCSTR p4ClientUser::TranslateFromBSTR(wstring &bs);
	BSTR p4ClientUser::TranslateToBSTR(StrBuf *sb);

	StrBufArray *InfoArray()
		{return m_InfoArray;};
	StrBufArray *ErrorArray() 
		{return m_ErrorArray;};
	StrBufArray *WarningArray()
		{return m_WarningArray;};
	virtual StrBuf TextFileName()
		{return m_TextFileName;};
	virtual StrBuf BinaryFileName()
		{return m_BinaryFileName;};

	virtual void SetInput(StrBuf *newVal)
		{m_input.Set(*newVal);};

	virtual void CloseFiles();	// closes open files (if there were any)

protected:
	int m_TranslateCharset;

private:
	SpecDataTable *m_specData;
	Spec *m_spec;
	StrBuf m_input;

	// Buffers for translations
	LPSTR m_tranbuf;	
	StrBuf m_buf;

	// Arrays used to return text strings to VB functions
	StrBufArray *m_ErrorArray;
	StrBufArray *m_InfoArray;
	StrBufArray *m_WarningArray;

	// These are pointers to temporary files used by OutputText and OutputBinary
	StrBuf m_BinaryFileName;
	FileSys *m_BinaryFile;
	StrBuf m_TextFileName;
	FileSys *m_TextFile;

	void OutputData(FileSys ** OutFile, StrBuf& FileName, enum FileSysType FType, 
				char *data, int len);

	void AddToArray(char *data, int len, StrBufArray *sba);
};

#endif // !defined(AFX_p4clientuser_H__C7A92243_2012_4DCA_8EB2_2E73ACA85A30__INCLUDED_)
